##############################################################
### Replication Code for 
### Christian B. Jensen, Sven-Oliver Proksch, and 
### Jonathan B. Slapin. 2013. Parliamentary Questions, 
### Oversight, and National Opposition Status in the 
### European Parliament”, Legislative Studies Quarterly 
### 38(2): 259-282. DOI: 10.1111/lsq.12013
##############################################################

rm(list=ls())
library(foreign)
library(Zelig) # Code written for Zelig version 3.5.4
library(memisc)

sample<-read.dta("JPS Parliamentary Questions LSQ 2013 Replication Data.dta")


###############################
# Figure 3 
# Government-Opposition Status
# and Legislative Oversight
###############################
	
alllegopp<-table(sample$opposition)
leginforceopp<-table(sample$opposition[sample$leginforcedummy==1])
alllegoppprop<-prop.table(alllegopp)
leginforceoppprop<-prop.table(leginforceopp)	
fig3data<-rbind(alllegoppprop, leginforceoppprop)
	
sum(alllegopp)
sum(leginforceopp)
	
# Plot
par(las=1,mar=c(7,4,1,1),xpd=FALSE)
barplot(fig3data,names=c("Government Parties","Opposition Parties"),ylim=c(0,1),col=c("grey80","grey30"),beside=TRUE,ylab="Proportion of parliamentary questions",cex.names=1.1)
abline(h=0.585,lwd=3,lty="dashed")
text(1,0.6,"Proportion of Opposition MEPs in EP",cex=0.7,adj=0)
par(xpd=TRUE)
legend(2.3,-0.15,c("All questions (n = 1,024)","Only questions on legislation in force (n = 412)"),fill=c("grey80","grey30"),cex=0.8)



###############################
### Table 1 
### Logit Models of Legislative
### Oversight
###############################


#########################################
# Model 1 (full sample of parl questions)
#########################################

sample$mepgroupfactor<-as.factor(sample$mepgroup)


logitfull<-zelig(leginforcedummy~ MEPmentionsowncountryreg +opposition+mepcomsameportfolio+mepcomsamestate+mepcomsamegroup+mepgroupfactor, data=sample,model="logit")

summary(logitfull)

#########################################
# Model 2 (parl questions from 
# mainstream parties only)
#########################################

samplemainstream<-sample[sample$mepgroupfactor=="EPP-ED"|sample$mepgroupfactor=="PES"|sample$mepgroupfactor=="ALDE"|sample$mepgroupfactor=="Greens/EFA",]

samplemainstream$mepgroupfactor<-factor(samplemainstream$mepgroupfactor)

logitfullmainstream<-zelig(leginforcedummy~ MEPmentionsowncountryreg +opposition+mepcomsameportfolio+mepcomsamestate+mepcomsamegroup+ mepgroupfactor, data=samplemainstream,model="logit")

summary(logitfullmainstream)

#########################################
# Model 3 (parl questions from 
# parties switching gov/opp status)
########################################	

# National Party Switch Status

tabopp<-table(sample$mepnatpartycode,sample$opposition)
natpartyoppswitch<-ifelse(tabopp[,1]!=0 & tabopp[,2]!=0,1,0)
natpartyoppswitch<-subset(natpartyoppswitch,natpartyoppswitch==1)
switchsubset<-sample[sample$mepnatpartycode %in% as.numeric(names(natpartyoppswitch)),]

table(switchsubset$opposition,switchsubset$leginforcedummy)

table(switchsubset$opposition,switchsubset$MEPmentionsowncountryreg)

# exclude 5 questions from Lega Nord (NI/IND-DEM group)
switchsubset<-switchsubset[switchsubset$mepgroup!="NI",]
switchsubset<-switchsubset[switchsubset$mepgroup!="IND/DEM",]

switchsubset$mepgroupfactor<-as.factor(switchsubset$mepgroup)
switchsubset$mepnatpartycodefac<-as.factor(switchsubset$mepnatpartycode)

logitmod<-zelig(leginforcedummy~ MEPmentionsowncountryreg +opposition+mepcomsameportfolio+mepcomsamestate+mepcomsamegroup+ mepnatpartycodefac,data=switchsubset,model="logit")

summary(logitmod)

###############################
### Table 2
### Predicted Probability of
### Oversight Question
###############################


#CDU 301
xlow<-setx(logitmod,opposition=0,mepnatpartycodefac=301)
xhigh<-setx(logitmod,opposition=1,mepnatpartycodefac=301)
out<-sim(logitmod,x=xlow,x1=xhigh)
summary(out)	


oppYmsY<-setx(logitmod,opposition=1, MEPmentionsowncountryreg=1, mepnatpartycodefac=301)
oppYmsN<-setx(logitmod,opposition=1, MEPmentionsowncountryreg=0, mepnatpartycodefac=301)
oppNmsY<-setx(logitmod,opposition=0, MEPmentionsowncountryreg=1, mepnatpartycodefac=301)
oppNmsN<-setx(logitmod,opposition=0, MEPmentionsowncountryreg=0, mepnatpartycodefac=301)

out.oppYmsY <-sim(logitmod,x= oppYmsY)
out.oppYmsN<-sim(logitmod,x= oppYmsN)
out.oppNmsY<-sim(logitmod,x= oppNmsY)
out.oppNmsN<-sim(logitmod,x= oppNmsN)

simres<-matrix(NA,4,3)
rownames(simres)<-c("oppYmsY","oppYmsN","oppNmsY","oppNmsN")
colnames(simres)<-c("Mean","CI LO","CI HI")
simres[1,1]<-apply(out.oppYmsY$qi$ev,2,mean)
simres[1,2:3]<-apply(out.oppYmsY$qi$ev,2,quantile,prob=c(0.025,0.975))
simres[2,1]<-apply(out.oppYmsN$qi$ev,2,mean)
simres[2,2:3]<-apply(out.oppYmsN$qi$ev,2,quantile,prob=c(0.025,0.975))
simres[3,1]<-apply(out.oppNmsY$qi$ev,2,mean)
simres[3,2:3]<-apply(out.oppNmsY$qi$ev,2,quantile,prob=c(0.025,0.975))
simres[4,1]<-apply(out.oppNmsN$qi$ev,2,mean)
simres[4,2:3]<-apply(out.oppNmsN$qi$ev,2,quantile,prob=c(0.025,0.975))

# Create table
round(simres,digits=2)